package com.ty.game;

import com.ty.game.config.YmlLoader;
import com.ty.game.dao.LogT2Dao;
import com.ty.game.dao.LogTestDao;
import com.ty.game.entity.LogT2;
import com.ty.game.entity.LogTest;
import com.ty.game.lang.SqlBatch;
import com.ty.game.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

/**
 * @author: Troy.Chen(失足程序员, 15388152619)
 * @version: 2021-07-15 12:06
 **/
@EnableAsync
@ConfigurationPropertiesScan
@MapperScan(basePackages = {"com.ty.game.dao"})
@ServletComponentScan(basePackages = {"com.ty.game.filter"})/*这个是使filter生效*/
@SpringBootApplication/*spring 启动标记*/
@Slf4j
public class ApplicationStart {


    public static void main(String[] args) throws IOException, InterruptedException {

        HashSet<String> argsSet = new HashSet<>();
        for (String arg : args) {
            argsSet.add(arg);
        }

        String ymlPath = "config/application.yml";

        File ymlFile = new File(ymlPath);
        if (ymlFile.exists()) {
            final Properties properties = YmlLoader.properties(ymlPath);
            for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
                System.out.println(objectObjectEntry.getKey() + "=" + objectObjectEntry.getValue());
            }
            /*可以指定文件，也可以指定目录，todo 如果是目录结尾加 / 一定的加*/
            argsSet.add("--spring.config.location=" + ymlFile.getParentFile().getPath() + File.separator);
        }

        String[] newArgs = argsSet.toArray(new String[argsSet.size()]);
        SpringUtil.CurApplicationContext = SpringApplication.run(ApplicationStart.class, newArgs);

        final long nanoTime = System.nanoTime();
        try (final SqlBatch sqlBatch = new SqlBatch()) {

            final LogT2 logT2 = new LogT2().setId((int) (System.currentTimeMillis() / 1000)).setName("1");
            sqlBatch.insert(LogT2Dao.class, logT2);

            final LogTest logTest = new LogTest().setId((int) (System.currentTimeMillis() / 1000)).setName("1").setName2("1");
            sqlBatch.insert(LogTestDao.class, logTest);

            sqlBatch.commit();
        }

        System.out.println("第一次插入完成 耗时：" + (((System.nanoTime() - nanoTime) / 10000) / 100f) + " ms");
        Thread.sleep(10000);

        try (final SqlBatch sqlBatch = new SqlBatch()) {

            final LogT2 logT2 = new LogT2().setId((int) (System.currentTimeMillis() / 1000)).setName("2");
            sqlBatch.insert(LogT2Dao.class, logT2);

            final LogTest logTest = new LogTest().setId((int) (System.currentTimeMillis() / 1000)).setName("2").setName2("2");
            sqlBatch.insert(LogTestDao.class, logTest);

            System.out.println("第二次插入等待 commit()");
            Thread.sleep(15000);
            sqlBatch.commit();
            System.out.println("第二次插入 commit() 结束");

            System.out.println("测试update");
            logT2.setName("333");
            sqlBatch.update(LogT2Dao.class, logT2);
            logTest.setName("3333");
            sqlBatch.update(LogTestDao.class, logTest);

            System.out.println("等待提交 update");
            Thread.sleep(15000);
            sqlBatch.commit();
            System.out.println("update end");
        }

    }


}
